﻿Public Class UCHoaDon

    Sub ddlKhachhangload()
        ddlKhachhang.DataSource = KhachHangBUS.KhachHang_Load
        ddlKhachhang.DisplayMember = "MAKH"
        ddlKhachhang.ValueMember = "MAKH"
    End Sub

    Sub GridView_Load()
        Dim sql As String = "select * from KHACHHANG k, HOADON h, nha n where h.MAKH = k.MAKH and k.MANHA = n.MANHA"
        GControl.DataSource = DataProvider.LoadTable(sql)

    End Sub
    Function TestInfo() As Boolean
        If txtNgaylap.Text = "" Or txttongtien.Text = "" Then
            ThongBao.TestInfo()
            Return False
        End If
        Return True
    End Function

    Private Sub UCHoaDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtNgaylap.Text = DateTime.Now.ToShortDateString
        ddlKhachhangload()
        GridView_Load()
    End Sub

    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        If Not TestInfo() Then
            Return
        End If
        Dim h As New HoaDonDTO
        'h.Mahoadon = 
        h.Makh = ddlKhachhang.SelectedValue
        h.Ngaylap = txtNgaylap.Text
        h.Tongtien = txttongtien.Text
        HoaDonBUS.HoaDon_Insert(h)
        GridView_Load()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        If Not TestInfo() Then
            Return
        End If
        If Not ThongBao.Agree Then
            Return
        End If
        Dim h As New HoaDonDTO
        h.Mahoadon = GView.GetFocusedRowCellDisplayText("MAHOADON")
        h.Makh = ddlKhachhang.SelectedValue
        h.Ngaylap = txtNgaylap.Text
        h.Tongtien = txttongtien.Text
        HoaDonBUS.HoaDon_Update(h)
        GridView_Load()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If Not ThongBao.Agree Then
            Return
        End If
        Dim h As New HoaDonDTO
        h.Mahoadon = GView.GetFocusedRowCellDisplayText("MAHOADON")
        HoaDonBUS.HoaDon_Delete(h)
        GridView_Load()
    End Sub

    Private Sub GView_RowCellClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs) Handles GView.RowCellClick
        txtMaHD.Text = GView.GetFocusedRowCellDisplayText("MAHOADON")
        txttongtien.Text = GView.GetFocusedRowCellDisplayText("TONGTIEN")
        txtNgaylap.Text = GView.GetFocusedRowCellDisplayText("NGAYLAP")
        ddlKhachhang.SelectedValue = GView.GetFocusedRowCellValue("MAKH")
    End Sub

    Private Sub btnExpotxls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        GControl.ShowPrintPreview()
        'GControl.

    End Sub

    Private Sub ddlKhachhang_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlKhachhang.SelectedIndexChanged
        If ddlKhachhang.Text = "" Then
            Return
        End If
        Dim maKH As String = ddlKhachhang.Text
        Dim sql As String = String.Format("select n.MUCGIA from KHACHHANG k , NHA n where n.MANHA = k.MANHA and k.MAKH = '{0}'", maKH)
        Dim dt As DataTable = DataProvider.LoadTable(sql)
        If dt.Rows.Count = 0 Then
            Return
        End If
        Dim dr As DataRow = dt.Rows(0)
        Dim giaphongthue As String = dr("MUCGIA").ToString
        txtGiaphong.Text = giaphongthue
        txttongtien.Text = giaphongthue
    End Sub

    Private Sub btnTongTien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTongTien.Click
        Dim sum As Decimal
        If txtPhuThu.Text ="" Then
            sum = Convert.ToDecimal(txtGiaphong.Text)
        Else
            sum = Convert.ToDecimal(txtGiaphong.Text) + Convert.ToDecimal(txtPhuThu.Text)
        End If
        txttongtien.Text = sum.ToString
    End Sub
End Class
